/*
	Copyright 2008, 2009, 2010 Brightworks, Inc.
	
	This file is part of Language Mentor.
	
	Language Mentor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	Language Mentor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Language Mentor.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.langcollab.languagementor.controller.audio
{
    import com.brightworks.util.Debug;

    import flash.events.Event;
    import flash.events.EventDispatcher;

    [Event(name="elementCompleteReport",  type="com.langcollab.languagementor.controller.audio.AudioProgressEvent" )]
    [Event(name="complete", type="flash.events.Event" )]

    public class AudioSequenceElement extends EventDispatcher
    {
        public var id:Object;
        public var isLeaf:Boolean = false;
        public var isPaused:Boolean = false;
        public var isPlaying:Boolean = false;
        public var levelId:String;

        public function AudioSequenceElement()
        {
        }

        // ****************************************************
        //
        //          Public Methods
        //
        // ****************************************************

        public function dispose():void
        {
			Debug.error_Fatal("AudioSequenceElement.dispose(), an abstract method, called");
        }

        public function pause(levelIdForLevelToRestartFromBeginning:String):void
        {
            if (!isPlaying) 
				Debug.error_Fatal(["AudioSequenceElement.pause(): called when not playing. this:", this]);
            if (isPaused)   
				Debug.error_Fatal(["AudioSequenceElement.pause(): called when paused. this:", this]);
            isPaused = true;
        }

        public function resume(levelIdForLevelToRestartFromBeginning:String):void
        {
            if (!isPlaying) 
				Debug.error_Fatal(["AudioSequenceElement.resume(): called when not playing. this:", this]);
            if (!isPaused)  
				Debug.error_Fatal(["AudioSequenceElement.resume(): called when not paused. this:", this]);
            isPaused = false;
        }

        public function start():void
        {
            if (isPlaying) 
				Debug.error_Fatal(["AudioSequenceElement.start(): called when already playing. this:", this]);
            isPlaying = true;
        }

        public function stop():void
        {
            if (!isPlaying) 
				Debug.error_Fatal(["AudioSequenceElement.stop(): called when not playing. this:", this]);
            isPaused  = false;
            isPlaying = false;
        }

        // ****************************************************
        //
        //          Protected Methods
        //
        // ****************************************************

        protected function onAllElementsComplete():void
        {
            isPlaying = false;
            // Dispatch ELEMENT_COMPLETE_REPORT - AudioController listens for this
            dispatchEvent(new AudioProgressEvent(AudioProgressEvent.ELEMENT_COMPLETE_REPORT, id, levelId));
            // Dispatch COMPLETE - immediate container listens for this
            dispatchEvent(new Event(Event.COMPLETE));
        }

        protected function onElementComplete(event:Event):void
        {
            // Abstract method: Different subclasses need to handle this in different ways.
			Debug.error_Fatal("AudioSequenceElement.onElementComplete(), an abstract method, called");
        }
    }
}